#include "TranItem.h"

TranItem::TranItem(int type, QWidget *parent):
	QWidget(parent)
{
    SetupUI();
    Initialize(type, false, 0);
}

TranItem::~TranItem()
{

}

// init m_tranSetting struct
void TranItem::Initialize(int type, bool isOn, int ratio)
{
    m_type = type;
	m_tranSetting.isOn = isOn;
	m_tranSetting.ratio = ratio;

    // set check state
    if(isOn){
        checkBox->setCheckState(Qt::Checked);
    } else {
        checkBox->setCheckState(Qt::Unchecked);
    }

    // set tran name
    label->setText("tran name");

    // set ratio number
    QString str;
	str.setNum(m_tranSetting.ratio);
	lineEdit->setText(str);

	// connect signals and slots
    connect(checkBox, SIGNAL(stateChanged(int)), this, SLOT(CheckHandle(int)));
    connect(lineEdit, SIGNAL(editingFinished()), this, SLOT(RatioHandle()));
}

void TranItem::SetupUI()
{
    horizontalLayout = new QHBoxLayout(this);
    horizontalLayout->setContentsMargins(0, 0, 0, 0);
    //int left, right, top, bottom;
    //horizontalLayout->getContentsMargins(&left, &top, &right, &bottom);
    horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
    checkBox = new QCheckBox(this);
    checkBox->setObjectName(QString::fromUtf8("checkBox"));

    horizontalLayout->addWidget(checkBox);

    label = new QLabel(this);
    label->setObjectName(QString::fromUtf8("label"));
	label->setFixedWidth(200); // to maintain a clear look

    horizontalLayout->addWidget(label);

    lineEdit = new QLineEdit(this);
    lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
	lineEdit->setFixedWidth(50);
	lineEdit->setMaxLength(2);

    horizontalLayout->addWidget(lineEdit);

    label_2 = new QLabel(this);
    label_2->setObjectName(QString::fromUtf8("label_2"));

    horizontalLayout->addWidget(label_2);

	horizontalLayout->addStretch();
	checkBox->setText(QString());
	label->setText(QString());
	label_2->setText("%");
}

// slots
// emit when state changed
void TranItem::CheckHandle(int state)
{
	bool tmp = false;

	if(state == Qt::Checked)
		tmp = true;
	if(tmp != m_tranSetting.isOn){
		m_tranSetting.isOn = tmp;
        emit checkChanged(m_type, tmp);
	}
}

// emit when ratio changed
void TranItem::RatioHandle()
{
	QLineEdit *sender = (QLineEdit *)this->sender();
	int ratio = sender->text().toInt();
	if(ratio != m_tranSetting.ratio){
		m_tranSetting.ratio = ratio;
        emit ratioChanged(m_type, ratio);
	}
}

